package com.kaili.basic.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.kaili.basic.mapper.BaseAllottedStoreMapper;
import com.kaili.basic.pojo.dto.*;
import com.kaili.basic.pojo.entity.BaseAllottedStoreDetailEntity;
import com.kaili.basic.pojo.entity.BaseAllottedStoreEntity;
import com.kaili.basic.pojo.vo.BaseAllottedStoreDetailVO;
import com.kaili.basic.pojo.vo.BaseAllottedStoreExportVO;
import com.kaili.basic.service.BaseAllottedStoreDetailService;
import com.kaili.basic.service.BaseAllottedStoreService;
import com.kaili.basic.service.BaseStoreAccountsService;
import com.kaili.basic.service.BaseStoreStockService;
import com.kaili.common.exception.RestException;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.LoginDetailUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class BaseAllottedStoreServiceImpl extends ServiceImpl<BaseAllottedStoreMapper, BaseAllottedStoreEntity> implements BaseAllottedStoreService {

    @Autowired
    private BaseAllottedStoreDetailService baseAllottedStoreDetailService;

    @Autowired
    private BaseAllottedStoreMapper baseAllottedStoreMapper;

    @Autowired
    private BaseStoreAccountsService baseStoreAccountsService;

    @Autowired
    private BaseStoreStockService baseStoreStockService;

    @Override
    public Long queryStoreOrderNo() {
        return baseAllottedStoreMapper.queryStoreOrderNo();
    }

    @Override
    public RestStatus addAllottedStore(BaseAllottedStoreAddDTO addDto) {
        //插入挑拨记录
        BaseAllottedStoreEntity entity=new BaseAllottedStoreEntity();
        BeanUtils.copyProperties(addDto,entity);
        entity.setCreateTime(new Date());
        entity.setCreateBy(LoginDetailUtils.getCurrentEmployeeId().longValue());

        //统计数量
        for (BaseAllottedStoreDetailAddDTO detail:addDto.getDetailList()) {
            entity.setTotalTrays(entity.getTotalTrays()+detail.getTrays());
            entity.setTotalVo(entity.getTotalVo()+detail.getVolume());
            entity.setTotalWeight(entity.getTotalWeight()+detail.getWeight());
            entity.setAmount(entity.getAmount()+detail.getAmount());
        }
        this.insert(entity);

        //插入移库明细
        baseAllottedStoreDetailService.insertBatch(addDto.getDetailList(),addDto.getOrderNo());
        return CommonStatus.ADD_OK;
    }

    @Override
    public RestStatus updateAllottedStore(BaseAllottedStoreEditDTO editDTO) {
        //更新调拨记录
        BaseAllottedStoreEntity entity=new BaseAllottedStoreEntity();
        BeanUtils.copyProperties(editDTO,entity);
        entity.setModifyTime(new Date());
        entity.setModifyBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        //统计数量
        for (BaseAllottedStoreDetailAddDTO detail:editDTO.getDetailList()) {
            entity.setTotalTrays(entity.getTotalTrays()+detail.getTrays());
            entity.setTotalVo(entity.getTotalVo()+detail.getVolume());
            entity.setTotalWeight(entity.getTotalWeight()+detail.getWeight());
            entity.setAmount(entity.getAmount()+detail.getAmount());
        }
        //更新当前调拨记录
        this.updateById(entity);
        //删除原有的产品明细
        baseAllottedStoreDetailService.deleteByMap(new HashMap<String,Object>(){{put("order_no",editDTO.getOrderNo());}});
        baseAllottedStoreDetailService.insertBatch(editDTO.getDetailList(),editDTO.getOrderNo());
        return CommonStatus.UPDATE_OK;
    }

    @Override
    public Page<BaseAllottedStoreDetailVO> queryPageList(Page<BaseAllottedStoreDetailVO> page, BaseAllottedStoreQueryDTO queryDto) {
        List<BaseAllottedStoreDetailVO> list=baseAllottedStoreMapper.queryPageList(page,queryDto);
        page.setRecords(list);
        return page;
    }

    @Override
    public List<BaseAllottedStoreDetailVO> queryDetailByOrderNo(HashMap<String, Object> map) {
        return baseAllottedStoreMapper.queryDetailByOrderNo(map);
    }

    @Override
    public BaseAllottedStoreDetailVO queryAllottedStoreDetail(Map<String, Object> map) {
        BaseAllottedStoreDetailVO detailVO = baseAllottedStoreMapper.queryPageList(map);
        List<BaseAllottedStoreDetailVO> list = baseAllottedStoreMapper.queryDetailByOrderNo(new HashMap<String, Object>() {{
            put("orderNo", detailVO.getOrderNo());
        }});
        detailVO.setList(list);
        return detailVO;
    }

    @Override
    public List<BaseAllottedStoreExportVO> allottedStoreExport(BaseAllottedStoreQueryDTO queryDto) {
        return baseAllottedStoreMapper.allottedStoreExport(queryDto);
    }

    @Override
    public RestStatus auditAllottedStore(BaseAllottedStoreAuditDto auditDto) throws RestException {
        BaseAllottedStoreEntity entity = this.selectById(auditDto.getId());
        //已经做过审核不做处理
        if(!("0".equals(entity.getIsAuduit()))){
            return CommonStatus.UPDATE_OK;
        }
        //1.更改移库单为已审核
        entity.setIsAuduit(auditDto.getIsAudit());
        entity.setAuditBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        entity.setAuditTime(new Date());
        this.updateById(entity);

        //审核通过做正式移库操作
        List<BaseAllottedStoreDetailEntity> detailList = baseAllottedStoreDetailService.selectByMap(new HashMap<String, Object>() {{
            put("order_no", auditDto.getOrderNo());
        }});

        //2.更新原库存 如果存在库存不足则抛出异常
        Boolean flage = baseStoreStockService.updateStock(detailList);
        if(!flage){
            throw new RestException(CommonStatus.STOCK_NOT_HAVE);
        }

       //3.批量新增新库存
        baseStoreStockService.bathInsert(detailList,this.selectById(entity.getId()).getSupplierId());

        //生成台账记录
        baseStoreAccountsService.insertBath(detailList);
        return CommonStatus.ADD_OK;
    }

}
